/*
 * Copyright [duowan.com]
 * Web Site: http://www.duowan.com
 * Since 2005 - 2014
 */

package com.duowan.realtime.dao.impl;

import static com.duowan.common.util.ObjectUtils.isNotEmpty;

import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.duowan.common.util.ObjectUtils;
import com.duowan.common.util.page.Page;
import com.duowan.realtime.dao.BaseSpringJdbcDao;
import com.duowan.realtime.dao.BloomfilterInfoDao;
import com.duowan.realtime.model.BloomfilterInfo;
import com.duowan.realtime.query.BloomfilterInfoQuery;

/**
 * tableName: bloomfilter_info
 * [BloomfilterInfo] 的Dao操作 
 *  
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
*/
public class BloomfilterInfoDaoImpl extends BaseSpringJdbcDao implements BloomfilterInfoDao{
	
	private RowMapper<BloomfilterInfo> entityRowMapper = new BeanPropertyRowMapper<BloomfilterInfo>(getEntityClass());
	
	static final private String COLUMNS = "id,db_name,partition,create_time";
	static final private String SELECT_FROM = "select " + COLUMNS + " from bloomfilter_info";
	
	@Override
	public Class<BloomfilterInfo> getEntityClass() {
		return BloomfilterInfo.class;
	}
	
	@Override
	public String getIdentifierPropertyName() {
		return "id";
	}
	
	public RowMapper<BloomfilterInfo> getEntityRowMapper() {
		return entityRowMapper;
	}
	
	public void insert(BloomfilterInfo entity) {
		String sql = "insert into bloomfilter_info " 
			 + " (id,db_name,partition,create_time) " 
			 + " values "
			 + " (:id,:dbName,:partition,:createTime)";
		insertWithGeneratedKey(entity,sql); //for sqlserver:identity and mysql:auto_increment
		
		//其它主键生成策略
		//insertWithOracleSequence(entity,"sequenceName",sql); //oracle sequence: 
		//insertWithDB2Sequence(entity,"sequenceName",sql); //db2 sequence:
		//insertWithUUID(entity,sql); //uuid
		//insertWithAssigned(entity,sql); //手工分配
	}
	
	public int update(BloomfilterInfo entity) {
		if (ObjectUtils.isEmpty(getByUniqueKey(entity.getDbName(), entity.getPartition()))) {
			insert(entity);
			return 0;
		}
		String sql = "update bloomfilter_info set "
					+ " db_name=:dbName,partition=:partition,create_time=:createTime "
					+ " where  id = :id ";
		return getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
	}
	
	public int deleteById(int id) {
		String sql = "delete from bloomfilter_info where  id = ? ";
		return  getSimpleJdbcTemplate().update(sql,  id);
	}

	public BloomfilterInfo getById(int id) {
		String sql = SELECT_FROM + " where  id = ? ";
		return (BloomfilterInfo)DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),id));
	}
	

	public Page<BloomfilterInfo> findPage(BloomfilterInfoQuery query) {
		
		StringBuilder sql = new StringBuilder("select "+ COLUMNS + " from bloomfilter_info where 1=1 ");
		if(isNotEmpty(query.getId())) {
            sql.append(" and id = :id ");
        }
		if(isNotEmpty(query.getDbName())) {
            sql.append(" and db_name = :dbName ");
        }
		if(isNotEmpty(query.getPartition())) {
            sql.append(" and partition = :partition ");
        }
		if(isNotEmpty(query.getCreateTimeBegin())) {
		    sql.append(" and create_time >= :createTimeBegin ");
		}
		if(isNotEmpty(query.getCreateTimeEnd())) {
            sql.append(" and create_time <= :createTimeEnd ");
        }
		
        //sql.append(" order by :sortColumns ");
		
		return pageQuery(sql.toString(),query,getEntityRowMapper());				
	}

	@Override
	public List<BloomfilterInfo> getBloomfilterInfos() {
		String sql = SELECT_FROM;
		return getSimpleJdbcTemplate().query(sql, entityRowMapper);
	}

	@Override
	public BloomfilterInfo getByUniqueKey(String dbName, String partition) {
		String sql = SELECT_FROM + " where  db_name = ? and partition = ? ";
		return DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),dbName, partition));
	}
}
